<html>
<head><meta charset="utf-8"><title>Skip::try_rfold · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html">Skip::try_rfold</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197876920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197876920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197876920">(May 18 2020 at 00:13)</a>:</h4>
<p>Hmm, Skip::try_rfold makes no sense to me: <a href="https://github.com/rust-lang/rust/blob/d79f1bd31a1401b5d08096fcdf9a9eb23ddf95df/src/libcore/iter/adapters/mod.rs#L1985-L2008">https://github.com/rust-lang/rust/blob/d79f1bd31a1401b5d08096fcdf9a9eb23ddf95df/src/libcore/iter/adapters/mod.rs#L1985-L2008</a></p>



<a name="197876921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197876921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197876921">(May 18 2020 at 00:13)</a>:</h4>
<p>Why doesn't it use <code>self.n</code>? What is going on with <code>n</code> inside check?</p>



<a name="197881404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197881404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197881404">(May 18 2020 at 01:57)</a>:</h4>
<p><code>try_rfold</code> works from the back of the iterator, not the front. If we take <code>n = self.len()</code> elements from the back of the iterator, we have reached the first item that needs to be skipped.</p>



<a name="197886449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886449">(May 18 2020 at 03:57)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> What really confuses me there is the apparent capture of <code>n</code> in the closure within <code>check</code>. I really don't understand how that can possibly work</p>



<a name="197886501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886501">(May 18 2020 at 03:58)</a>:</h4>
<p>Maybe because it's a <code>move</code> closure?</p>



<a name="197886587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886587">(May 18 2020 at 04:01)</a>:</h4>
<p>That's right. As for why it's written in such a convoluted way, I have no idea.</p>



<a name="197886635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886635">(May 18 2020 at 04:02)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> Many iterator functions go through <code>try_fold</code>/<code>try_rfold</code>, because LLVM ends up optimizing them well</p>



<a name="197886636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886636">(May 18 2020 at 04:02)</a>:</h4>
<p>sometimes this results in them being hard to read</p>



<a name="197886641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886641">(May 18 2020 at 04:02)</a>:</h4>
<p>Oh no. I meant putting the closure inside of <code>check</code> instead of passing it directly.</p>



<a name="197886644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886644">(May 18 2020 at 04:03)</a>:</h4>
<p>Presumably to reduce the number of in-scope generic parameters for the closure?</p>



<a name="197886647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197886647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> njn <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197886647">(May 18 2020 at 04:03)</a>:</h4>
<p>That results in less LLVM IR being generated, reducing compile times,  believe it or not. It's a pattern used all throughout the iterator code</p>



<a name="197958227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197958227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197958227">(May 18 2020 at 16:16)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> reducing generic parameters is a big part of it, yes, since <a href="https://github.com/rust-lang/rust/pull/62429">https://github.com/rust-lang/rust/pull/62429</a></p>



<a name="197959749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197959749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197959749">(May 18 2020 at 16:26)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="138448">@cuviper</span>. I checked <code>git blame</code> shortly after the discussion above, so I saw that PR. I was initially confused because <code>fold</code> is still generic and will almost always be a unique type, so presumably this doesn't reduce the number of instantiations of the closure. I read another comment from you that <a href="https://github.com/rust-lang/rust/issues/62429">#62429</a> was mainly to avoid running into type-length limit by getting <code>I</code> out of scope, which makes more sense.</p>



<a name="197960009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197960009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197960009">(May 18 2020 at 16:28)</a>:</h4>
<p>Otherwise I was going to suggest that <span class="user-mention silent" data-user-id="116107">davidtwco</span> revert this to get a better idea of the impact of <a href="https://github.com/rust-lang/rust/issues/69749">#69749</a>.</p>



<a name="197960020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197960020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197960020">(May 18 2020 at 16:28)</a>:</h4>
<p>Right, it was mostly motivated by type length</p>



<a name="197960174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197960174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197960174">(May 18 2020 at 16:29)</a>:</h4>
<p>it could still reduce for <code>fold</code> with things like <code>Chain&lt;I1, I2&gt;</code>, then you might be forwarding fold with the same closure yet different iterator types, but internally they have the same <code>::Item</code></p>



<a name="197960222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Skip%3A%3Atry_rfold/near/197960222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Skip.3A.3Atry_rfold.html#197960222">(May 18 2020 at 16:29)</a>:</h4>
<p>Well, a more advanced version of <a href="https://github.com/rust-lang/rust/issues/69749">#69749</a> I guess, I think that's only for functions that don't depend on <em>any</em> of their generic parameters.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>